#include <stdio.h>
#include <stdbool.h>
#include <string.h>
#include <stdlib.h>

bool equationsPossible(char** equations, int equationsSize) {
	char **now = equations;
	char (*lit)[26] = malloc(26 * 26 * sizeof(char));
	for (int i = 0; i < 26; i++) {
		memset(lit[i], '0', 26 * sizeof(char));
		lit[i][i] = '1';
	}
	for (int i = 0; i < equationsSize; i++, now++) {
		printf("%s\n", *now);
		if (strlen((*now)) != 4) {
			printf("%d", __LINE__);
			return false;
		}
		int a = (*now)[0] - 'a';
		int b = (*now)[3] - 'a';
		if (a < 0 || a > 25 || b < 0 || b > 25) {
			printf("%d", __LINE__);
			return false;
		}
		if ((*now)[2] != '=') {
			printf("%d", __LINE__);
			return false;
		}
		switch ((*now)[1]) {
			case '!':
				if (lit[a][b] == '1') {
					return false;
				}
				if (lit[a][b] == '0') {
					lit[a][b] = '2';
					lit[b][a] = '2';
				}
				for (int i = 0; i < 26; i++) {
					if (lit[a][i] == '1') {
						if (lit[b][i] == '1') {
							return false;
						}
						if (lit[b][i] == '0') {
							lit[b][i] = '2';
							lit[i][b] = '2';
						}
					}
				}
				break;
			case '=':
				if (a == b) {
					continue;
				}
				if (lit[a][b] == '2'){
					return false;
				}
				if (lit[a][b] == '0') {
					lit[a][b] = '1';
					lit[b][a] = '1';
					for (int i = 0; i < 26; i++) {
						switch (lit[a][i]) {
							case '1':
								if (lit[b][i] == '2') {
									return false;
								}
								if (lit[b][i] == '0') {
									lit[b][i] = '1';
									lit[i][b] = '1';
								}
								break;
							case '0':
								if (lit[b][i] == '1') {
									lit[a][i] = '1';
									lit[i][a] = '1';
								} else if (lit[b][i] == '2') {
									lit[a][i] = '2';
									lit[i][a] = '2';
								}
								break;
							case '2':
								if (lit[b][i] == '1') {
									return false;
								}
								if (lit[b][i] == '0') {
									lit[b][i] = '2';
									lit[i][b] = '2';
								}
								break;
						}
					}
				}
				break;
			default:
				printf("%d", __LINE__);
				return false;
				break;
		}
	}
	return true;
}

int main() {
	char *equations[4] = {"a==b", "e==c", "b==c", "a!=e"};
	printf("%d", equationsPossible(equations, 4));
}
